package com.projectName.www.servlet;

import com.projectName.www.po.User;
import com.projectName.www.service.UserService;
import com.projectName.www.service.UserServiceImpl;
import com.projectName.www.util.MD5Util;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/updateUserServlet")
public class UpdateUserServlet extends HttpServlet {


        protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            request.setCharacterEncoding("utf-8");
            //1.获取id
            String userName = request.getParameter("userName");
            String password = MD5Util.string2MD5(request.getParameter("password"));
            String address = request.getParameter("address");
            String contact = request.getParameter("contact");
            String userType = request.getParameter("userType");
            String jianjie = request.getParameter("jianjie");

            String uid=request.getParameter("uid");
            System.out.println("uid="+uid);
            //3.封装对象
            /**
             * 判断密码是否相同
             * 1.通过uid查原来密码
             * 2.不是原来的密码就加密后再放进去
             */

            UserService us=new UserServiceImpl();

            User user = us.findByID(uid);

            user.setUserName(userName);
            user.setPassword(password);
            user.setUserType(Integer.parseInt(userType));
            user.setContact(contact);
            user.setAddress(address);
            user.setJianjie(jianjie);

            //4.调用Service保存
            UserService service = new UserServiceImpl();
            service.updateUser(user);

            //5.跳转到userListServlet
            response.sendRedirect(request.getContextPath()+"/userListServlet");
        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
}

